package com.shrct.businessprocess.purchase.impl;

import com.google.common.base.Objects;
import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering;
import com.shrct.businessprocess.budget.RCBudget;
import com.shrct.businessprocess.budget.RCCost4ACTItem;
import com.shrct.businessprocess.budget.RCCostBreakDown;
import com.shrct.businessprocess.common.CurrencyType;
import com.shrct.businessprocess.common.Orderings;
import com.shrct.businessprocess.money.Moneys;
import com.shrct.businessprocess.organization.RCSupplier;
import com.shrct.businessprocess.organization.impl.RCSupplierImpl;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProject;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.project.RCRealMoney;
import com.shrct.businessprocess.project.impl.RCExchangeRateImpl;
import com.shrct.businessprocess.purchase.RCBasePurchaseOrder;
import com.shrct.businessprocess.purchase.RCBasePurchaseOrderAccept;
import com.shrct.businessprocess.purchase.RCBasePurchasePayRequest;
import com.shrct.businessprocess.purchase.RCBasePurchaseRequest;
import com.shrct.businessprocess.purchase.RCHasPurchaseBudget;
import com.shrct.businessprocess.purchase.RCOrderCommissionPayRequest;
import com.shrct.businessprocess.purchase.RCPay;
import com.shrct.businessprocess.purchase.RCPurchaseOrderInvoice;
import com.shrct.businessprocess.purchase.RCPurchaseOrderProcess;
import com.shrct.businessprocess.purchase.RCPurchasePayRequest;
import com.shrct.businessprocess.purchase.impl.RCBasePurchasePayRequestImpl;
import com.shrct.businessprocess.purchase.impl.RCOrderCommissionPayRequestImpl;
import com.shrct.businessprocess.purchase.impl.RCPurchaseOrderInvoiceImpl;
import com.shrct.businessprocess.purchase.impl.RCPurchasePayRequestImpl;
import com.shrct.businessprocess.purchase.proxy.RCBasePurchaseOrderProxy;
import edu.fudan.mylang.common.NumberExtensions;
import edu.fudan.mylang.pf.BaseModelObject;
import edu.fudan.mylang.pf.IGenericQuery;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.DoubleExtensions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.Functions.Function2;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
import org.hibernate.annotations.DiscriminatorOptions;

@Entity
@DiscriminatorValue("RCBASEPURCHASEORDER_TYPE")
@DiscriminatorOptions(force = true)
@DiscriminatorColumn(length = 64)
@Table(name = "rcbasepurchaseorder")
public abstract class RCBasePurchaseOrderImpl extends BaseModelObject implements RCBasePurchaseOrder {
  public RCBasePurchaseOrderImpl() {
    super();
  }
  
  private String orderNumber;
  
  public String getOrderNumber() {
    return this.orderNumber;
  }
  
  public RCBasePurchaseOrder setOrderNumber(final String orderNumber) {
    this.orderNumber = orderNumber;
    return this;			
    
  }
  
  private Boolean isInitialized = Boolean.valueOf(false);
  
  public Boolean getIsInitialized() {
    return this.isInitialized;
  }
  
  public RCBasePurchaseOrder setIsInitialized(final Boolean isInitialized) {
    this.isInitialized = isInitialized;
    return this;			
    
  }
  
  private String payMethodStr;
  
  public String getPayMethodStr() {
    return this.payMethodStr;
  }
  
  public RCBasePurchaseOrder setPayMethodStr(final String payMethodStr) {
    this.payMethodStr = payMethodStr;
    return this;			
    
  }
  
  @JoinColumn(name = "supplier_id")
  @ManyToOne(targetEntity = RCSupplierImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCSupplier supplier;
  
  public RCSupplier getSupplier() {
    return this.supplier;
  }
  
  public RCBasePurchaseOrder setSupplier(final RCSupplier supplier) {
    this.supplier = supplier;
    return this;			
    
  }
  
  private Date initializeDate;
  
  public Date getInitializeDate() {
    return this.initializeDate;
  }
  
  public RCBasePurchaseOrder setInitializeDate(final Date initializeDate) {
    this.initializeDate = initializeDate;
    return this;			
    
  }
  
  private Date closeDate;
  
  public Date getCloseDate() {
    return this.closeDate;
  }
  
  public RCBasePurchaseOrder setCloseDate(final Date closeDate) {
    this.closeDate = closeDate;
    return this;			
    
  }
  
  private Integer term;
  
  public Integer getTerm() {
    return this.term;
  }
  
  public RCBasePurchaseOrder setTerm(final Integer term) {
    this.term = term;
    return this;			
    
  }
  
  private Boolean isClosed = Boolean.valueOf(false);
  
  public Boolean getIsClosed() {
    return this.isClosed;
  }
  
  public RCBasePurchaseOrder setIsClosed(final Boolean isClosed) {
    this.isClosed = isClosed;
    return this;			
    
  }
  
  private Double taxRate = Double.valueOf(0d);
  
  public Double getTaxRate() {
    return this.taxRate;
  }
  
  public RCBasePurchaseOrder setTaxRate(final Double taxRate) {
    this.taxRate = taxRate;
    return this;			
    
  }
  
  @JoinColumn(name = "exchangeRate_id")
  @ManyToOne(targetEntity = RCExchangeRateImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST })
  private RCExchangeRate exchangeRate;
  
  public RCExchangeRate getExchangeRate() {
    return this.exchangeRate;
  }
  
  public RCBasePurchaseOrder setExchangeRate(final RCExchangeRate exchangeRate) {
    this.exchangeRate = exchangeRate;
    return this;			
    
  }
  
  public RCProjectMoney getTax() {
    RCProjectMoney _totalCost = this.getTotalCost();
    Double _nullsafe = NumberExtensions.nullsafe(this.taxRate);
    double _divide = ((_nullsafe).doubleValue() / 100);
    double _plus = (1 + _divide);
    RCProjectMoney _div = _totalCost.div(Double.valueOf(_plus));
    Double _nullsafe_1 = NumberExtensions.nullsafe(this.taxRate);
    double _divide_1 = ((_nullsafe_1).doubleValue() / 100);
    RCProjectMoney _multi = _div.multi(Double.valueOf(_divide_1));
    return _multi;
  }
  
  /**
   * 付款申请总金额
   */
  public RCProjectMoney getTotalRequestPay() {
    final Function1<RCBasePurchasePayRequest,RCProjectMoney> _function = new Function1<RCBasePurchasePayRequest,RCProjectMoney>() {
        public RCProjectMoney apply(final RCBasePurchasePayRequest it) {
          RCProjectMoney _amount = it.getAmount();
          return _amount;
        }
      };
    Iterable<RCProjectMoney> _map = IterableExtensions.<RCBasePurchasePayRequest, RCProjectMoney>map(this.payRequests, _function);
    RCExchangeRate _defaultExchangeRate = this.getDefaultExchangeRate();
    RCProjectMoney _sum = Moneys.sum(_map, _defaultExchangeRate);
    return _sum;
  }
  
  public RCOrderCommissionPayRequest createCommissionPayRequest() {RCOrderCommissionPayRequest rCOrderCommissionPayRequest=RCOrderCommissionPayRequestImpl.create(this,getObjectFactory());
    final Procedure1<RCOrderCommissionPayRequest> _function = new Procedure1<RCOrderCommissionPayRequest>() {
        public void apply(final RCOrderCommissionPayRequest it) {
          RCBudget _budget = it.getBudget();
          it.setThebudget(_budget);
          Date _date = new Date();
          it.setTheStartDate(_date);
        }
      };
    RCOrderCommissionPayRequest _doubleArrow = ObjectExtensions.<RCOrderCommissionPayRequest>operator_doubleArrow(rCOrderCommissionPayRequest, _function);
    return _doubleArrow;
  }
  
  /**
   * 以项目汇率和币种为结算方式的已支付金额
   */
  public RCProjectMoney getTotalPayedProjectAmount() {
    final Function1<RCBasePurchasePayRequest,Collection<RCPay>> _function = new Function1<RCBasePurchasePayRequest,Collection<RCPay>>() {
        public Collection<RCPay> apply(final RCBasePurchasePayRequest it) {
          Collection<RCPay> _purchasePays = it.getPurchasePays();
          return _purchasePays;
        }
      };
    Iterable<Collection<RCPay>> _map = IterableExtensions.<RCBasePurchasePayRequest, Collection<RCPay>>map(this.payRequests, _function);
    Iterable<RCPay> _flatten = Iterables.<RCPay>concat(_map);
    final Function1<RCPay,RCProjectMoney> _function_1 = new Function1<RCPay,RCProjectMoney>() {
        public RCProjectMoney apply(final RCPay it) {
          RCProjectMoney _projectAmount = it.getProjectAmount();
          return _projectAmount;
        }
      };
    Iterable<RCProjectMoney> _map_1 = IterableExtensions.<RCPay, RCProjectMoney>map(_flatten, _function_1);
    RCExchangeRate _defaultExchangeRate = this.getDefaultExchangeRate();
    RCProjectMoney _sum = Moneys.sum(_map_1, _defaultExchangeRate);
    return _sum;
  }
  
  public Boolean getIsAllMoneyPaid() {
    RCProjectMoney _totalCost = this.getTotalCost();
    Double _hostAmount = _totalCost.toHostAmount();
    long _longValue = _hostAmount.longValue();
    RCProjectMoney _totalPayedProjectAmount = this.getTotalPayedProjectAmount();
    Double _hostAmount_1 = _totalPayedProjectAmount.toHostAmount();
    long _longValue_1 = _hostAmount_1.longValue();
    boolean _lessEqualsThan = (_longValue <= _longValue_1);
    return Boolean.valueOf(_lessEqualsThan);
  }
  
  public RCRealMoney getTotalInvoiceAmount() {
    RCRealMoney _xblockexpression = null;
    {
      final Function1<RCPurchaseOrderInvoice,Double> _function = new Function1<RCPurchaseOrderInvoice,Double>() {
          public Double apply(final RCPurchaseOrderInvoice it) {
            RCRealMoney _realAmount = it.getRealAmount();
            Double _amount = _realAmount.getAmount();
            return _amount;
          }
        };
      Iterable<Double> _map = IterableExtensions.<RCPurchaseOrderInvoice, Double>map(this.invoices, _function);
      final Function2<Double,Double,Double> _function_1 = new Function2<Double,Double,Double>() {
          public Double apply(final Double d1, final Double d2) {
            double _plus = DoubleExtensions.operator_plus(d1, d2);
            return Double.valueOf(_plus);
          }
        };
      Double _reduce = IterableExtensions.<Double>reduce(_map, _function_1);
      final Double amountIngore = NumberExtensions.nullsafe(_reduce);
      final Function1<RCPurchaseOrderInvoice,Double> _function_2 = new Function1<RCPurchaseOrderInvoice,Double>() {
          public Double apply(final RCPurchaseOrderInvoice it) {
            RCRealMoney _realAmount = it.getRealAmount();
            Double _hostAmount = _realAmount.toHostAmount();
            return _hostAmount;
          }
        };
      Iterable<Double> _map_1 = IterableExtensions.<RCPurchaseOrderInvoice, Double>map(this.invoices, _function_2);
      final Function2<Double,Double,Double> _function_3 = new Function2<Double,Double,Double>() {
          public Double apply(final Double d1, final Double d2) {
            double _plus = DoubleExtensions.operator_plus(d1, d2);
            return Double.valueOf(_plus);
          }
        };
      Double _reduce_1 = IterableExtensions.<Double>reduce(_map_1, _function_3);
      final Double hostAmount = NumberExtensions.nullsafe(_reduce_1);
      RCRealMoney _rCRealMoney = new RCRealMoney();
      final Procedure1<RCRealMoney> _function_4 = new Procedure1<RCRealMoney>() {
          public void apply(final RCRealMoney it) {
            it.setAmount(amountIngore);
            RCExchangeRate _defaultExchangeRate = RCBasePurchaseOrderImpl.this.getDefaultExchangeRate();
            CurrencyType _currencyType = _defaultExchangeRate.getCurrencyType();
            it.setCurrencyType(_currencyType);
            double _divide = DoubleExtensions.operator_divide(hostAmount, amountIngore);
            it.setExchangeRate(Double.valueOf(_divide));
          }
        };
      RCRealMoney _doubleArrow = ObjectExtensions.<RCRealMoney>operator_doubleArrow(_rCRealMoney, _function_4);
      _xblockexpression = (_doubleArrow);
    }
    return _xblockexpression;
  }
  
  /**
   * 最近付款截止日
   */
  public Date getNearestPayDeadline() {
    Date _xblockexpression = null;
    {
      Collection<? extends RCBasePurchaseOrderAccept> _acceptItems = this.getAcceptItems();
      final Function1<RCBasePurchaseOrderAccept,Boolean> _function = new Function1<RCBasePurchaseOrderAccept,Boolean>() {
          public Boolean apply(final RCBasePurchaseOrderAccept it) {
            Boolean _isTotalPaid = it.getIsTotalPaid();
            boolean _not = (!_isTotalPaid);
            return Boolean.valueOf(_not);
          }
        };
      Iterable<? extends RCBasePurchaseOrderAccept> _filter = IterableExtensions.filter(_acceptItems, _function);
      final Function1<RCBasePurchaseOrderAccept,Date> _function_1 = new Function1<RCBasePurchaseOrderAccept,Date>() {
          public Date apply(final RCBasePurchaseOrderAccept it) {
            Date _termDate = it.getTermDate();
            return _termDate;
          }
        };
      Iterable<Date> _map = IterableExtensions.map(_filter, _function_1);
      final Iterable<Date> dates = IterableExtensions.<Date>filterNull(_map);
      Date _xifexpression = null;
      boolean _isEmpty = IterableExtensions.isEmpty(dates);
      if (_isEmpty) {
        _xifexpression = null;
      } else {
        Ordering<Date> _dateOrdering = Orderings.getDateOrdering();
        Collection<? extends RCBasePurchaseOrderAccept> _acceptItems_1 = this.getAcceptItems();
        final Function1<RCBasePurchaseOrderAccept,Boolean> _function_2 = new Function1<RCBasePurchaseOrderAccept,Boolean>() {
            public Boolean apply(final RCBasePurchaseOrderAccept it) {
              Boolean _isTotalPaid = it.getIsTotalPaid();
              boolean _not = (!_isTotalPaid);
              return Boolean.valueOf(_not);
            }
          };
        Iterable<? extends RCBasePurchaseOrderAccept> _filter_1 = IterableExtensions.filter(_acceptItems_1, _function_2);
        Iterable<? extends RCBasePurchaseOrderAccept> _filterNull = IterableExtensions.filterNull(_filter_1);
        final Function1<RCBasePurchaseOrderAccept,Date> _function_3 = new Function1<RCBasePurchaseOrderAccept,Date>() {
            public Date apply(final RCBasePurchaseOrderAccept it) {
              Date _termDate = it.getTermDate();
              return _termDate;
            }
          };
        Iterable<Date> _map_1 = IterableExtensions.map(_filterNull, _function_3);
        Iterable<Date> _filterNull_1 = IterableExtensions.<Date>filterNull(_map_1);
        Date _min = _dateOrdering.<Date>min(_filterNull_1);
        _xifexpression = _min;
      }
      _xblockexpression = (_xifexpression);
    }
    return _xblockexpression;
  }
  
  public Iterable<RCCost4ACTItem> getCost4ACItems() {
    Iterable<RCCost4ACTItem> _xblockexpression = null;
    {
      Iterable<RCCost4ACTItem> _xifexpression = null;
      boolean _or = false;
      RCRealMoney _totalInvoiceAmount = this.getTotalInvoiceAmount();
      Double _amount = _totalInvoiceAmount.getAmount();
      long _longValue = _amount.longValue();
      RCProjectMoney _totalCost = this.getTotalCost();
      Double _amount_1 = _totalCost.getAmount();
      long _longValue_1 = _amount_1.longValue();
      boolean _greaterEqualsThan = (_longValue >= _longValue_1);
      if (_greaterEqualsThan) {
        _or = true;
      } else {
        _or = (_greaterEqualsThan || (this.isClosed).booleanValue());
      }
      if (_or) {
        final Function1<RCPurchaseOrderInvoice,RCCost4ACTItem> _function = new Function1<RCPurchaseOrderInvoice,RCCost4ACTItem>() {
            public RCCost4ACTItem apply(final RCPurchaseOrderInvoice invoice) {
              RCCost4ACTItem _rCCost4ACTItem = new RCCost4ACTItem();
              final Procedure1<RCCost4ACTItem> _function = new Procedure1<RCCost4ACTItem>() {
                  public void apply(final RCCost4ACTItem it) {
                    RCBasePurchaseRequest _purchaseRequest = RCBasePurchaseOrderImpl.this.getPurchaseRequest();
                    RCHasPurchaseBudget _budget = _purchaseRequest.getBudget();
                    it.setBudget(_budget);
                    String _sid = invoice.getSid();
                    String _plus = ("\u53D1\u7968:" + _sid);
                    it.setTitle(_plus);
                    RCRealMoney _realAmount = invoice.getRealAmount();
                    Double _hostAmount = _realAmount.toHostAmount();
                    it.setAmount(_hostAmount);
                    Double _taxRate = invoice.getTaxRate();
                    it.setTaxRate(_taxRate);
                    Date _acceptDate = invoice.getAcceptDate();
                    it.setDate(_acceptDate);
                  }
                };
              RCCost4ACTItem _doubleArrow = ObjectExtensions.<RCCost4ACTItem>operator_doubleArrow(_rCCost4ACTItem, _function);
              return _doubleArrow;
            }
          };
        Iterable<RCCost4ACTItem> _map = IterableExtensions.<RCPurchaseOrderInvoice, RCCost4ACTItem>map(this.invoices, _function);
        _xifexpression = _map;
      } else {
        RCCost4ACTItem _rCCost4ACTItem = new RCCost4ACTItem();
        final Procedure1<RCCost4ACTItem> _function_1 = new Procedure1<RCCost4ACTItem>() {
            public void apply(final RCCost4ACTItem it) {
              RCBasePurchaseRequest _purchaseRequest = RCBasePurchaseOrderImpl.this.getPurchaseRequest();
              RCHasPurchaseBudget _budget = _purchaseRequest.getBudget();
              it.setBudget(_budget);
              String _plus = ("\u8BA2\u5355:" + RCBasePurchaseOrderImpl.this.orderNumber);
              it.setTitle(_plus);
              RCProjectMoney _totalCost = RCBasePurchaseOrderImpl.this.getTotalCost();
              Double _hostAmount = _totalCost.toHostAmount();
              it.setAmount(_hostAmount);
              it.setTaxRate(RCBasePurchaseOrderImpl.this.taxRate);
              it.setDate(RCBasePurchaseOrderImpl.this.initializeDate);
            }
          };
        RCCost4ACTItem _doubleArrow = ObjectExtensions.<RCCost4ACTItem>operator_doubleArrow(_rCCost4ACTItem, _function_1);
        ArrayList<RCCost4ACTItem> _newArrayList = CollectionLiterals.<RCCost4ACTItem>newArrayList(_doubleArrow);
        _xifexpression = _newArrayList;
      }
      final Iterable<RCCost4ACTItem> directItems = _xifexpression;
      Iterable<RCOrderCommissionPayRequest> _filter = Iterables.<RCOrderCommissionPayRequest>filter(this.payRequests, RCOrderCommissionPayRequest.class);
      final Function1<RCOrderCommissionPayRequest,Boolean> _function_2 = new Function1<RCOrderCommissionPayRequest,Boolean>() {
          public Boolean apply(final RCOrderCommissionPayRequest it) {
            Boolean _isFinished = it.getIsFinished();
            return _isFinished;
          }
        };
      Iterable<RCOrderCommissionPayRequest> _filter_1 = IterableExtensions.<RCOrderCommissionPayRequest>filter(_filter, _function_2);
      final Function1<RCOrderCommissionPayRequest,RCCost4ACTItem> _function_3 = new Function1<RCOrderCommissionPayRequest,RCCost4ACTItem>() {
          public RCCost4ACTItem apply(final RCOrderCommissionPayRequest commission) {
            RCCost4ACTItem _rCCost4ACTItem = new RCCost4ACTItem();
            final Procedure1<RCCost4ACTItem> _function = new Procedure1<RCCost4ACTItem>() {
                public void apply(final RCCost4ACTItem it) {
                  RCBasePurchaseRequest _purchaseRequest = RCBasePurchaseOrderImpl.this.getPurchaseRequest();
                  RCHasPurchaseBudget _budget = _purchaseRequest.getBudget();
                  it.setBudget(_budget);
                  String _plus = ("\u624B\u7EED\u8D39:" + RCBasePurchaseOrderImpl.this.orderNumber);
                  it.setTitle(_plus);
                  RCProjectMoney _amount = commission.getAmount();
                  Double _hostAmount = _amount.toHostAmount();
                  it.setAmount(_hostAmount);
                  Double _taxRate = commission.getTaxRate();
                  it.setTaxRate(_taxRate);
                  Date _finishDate = commission.getFinishDate();
                  it.setDate(_finishDate);
                }
              };
            RCCost4ACTItem _doubleArrow = ObjectExtensions.<RCCost4ACTItem>operator_doubleArrow(_rCCost4ACTItem, _function);
            return _doubleArrow;
          }
        };
      final Iterable<RCCost4ACTItem> commissionItems = IterableExtensions.<RCOrderCommissionPayRequest, RCCost4ACTItem>map(_filter_1, _function_3);
      ArrayList<Iterable<RCCost4ACTItem>> _newArrayList_1 = CollectionLiterals.<Iterable<RCCost4ACTItem>>newArrayList(directItems, commissionItems);
      Iterable<RCCost4ACTItem> _flatten = Iterables.<RCCost4ACTItem>concat(_newArrayList_1);
      _xblockexpression = (_flatten);
    }
    return _xblockexpression;
  }
  
  /**
   * 已支付总金额
   */
  public RCRealMoney getTotalPayedAmount() {
    RCRealMoney _xblockexpression = null;
    {
      RCBudget _budget = this.getBudget();
      RCCostBreakDown _cbd = _budget.getCbd();
      RCProject _project = _cbd.getProject();
      final CurrencyType hostCurrencyType = _project.getCurrencyType();
      final Function1<RCBasePurchasePayRequest,Collection<RCPay>> _function = new Function1<RCBasePurchasePayRequest,Collection<RCPay>>() {
          public Collection<RCPay> apply(final RCBasePurchasePayRequest it) {
            Collection<RCPay> _purchasePays = it.getPurchasePays();
            return _purchasePays;
          }
        };
      Iterable<Collection<RCPay>> _map = IterableExtensions.<RCBasePurchasePayRequest, Collection<RCPay>>map(this.payRequests, _function);
      Iterable<RCPay> _flatten = Iterables.<RCPay>concat(_map);
      final Function1<RCPay,RCRealMoney> _function_1 = new Function1<RCPay,RCRealMoney>() {
          public RCRealMoney apply(final RCPay it) {
            RCRealMoney _amount = it.getAmount();
            return _amount;
          }
        };
      Iterable<RCRealMoney> _map_1 = IterableExtensions.<RCPay, RCRealMoney>map(_flatten, _function_1);
      final Function2<RCRealMoney,RCRealMoney,RCRealMoney> _function_2 = new Function2<RCRealMoney,RCRealMoney,RCRealMoney>() {
          public RCRealMoney apply(final RCRealMoney d1, final RCRealMoney d2) {
            RCRealMoney _plus = d1.plus(d2, hostCurrencyType);
            return _plus;
          }
        };
      RCRealMoney value = IterableExtensions.<RCRealMoney>reduce(_map_1, _function_2);
      RCRealMoney _xifexpression = null;
      boolean _equals = Objects.equal(value, null);
      if (_equals) {
        RCRealMoney _rCRealMoney = new RCRealMoney();
        final Procedure1<RCRealMoney> _function_3 = new Procedure1<RCRealMoney>() {
            public void apply(final RCRealMoney it) {
              it.setAmount(Double.valueOf(0d));
              RCExchangeRate _defaultExchangeRate = RCBasePurchaseOrderImpl.this.getDefaultExchangeRate();
              CurrencyType _currencyType = _defaultExchangeRate.getCurrencyType();
              it.setCurrencyType(_currencyType);
              it.setExchangeRate(Double.valueOf(1d));
            }
          };
        RCRealMoney _doubleArrow = ObjectExtensions.<RCRealMoney>operator_doubleArrow(_rCRealMoney, _function_3);
        _xifexpression = _doubleArrow;
      } else {
        _xifexpression = value;
      }
      _xblockexpression = (_xifexpression);
    }
    return _xblockexpression;
  }
  
  public RCPurchaseOrderProcess getProcess() {
    IGenericQuery<RCPurchaseOrderProcess> query=getObjectFactory().createGenericQuery(RCPurchaseOrderProcess.class,"select p from com.shrct.businessprocess.purchase.impl.RCPurchaseOrderProcessImpl as p   where ((p.purchaseOrder) = (:this)) and  p.active = 1      ").setParameter("this",this);
    RCPurchaseOrderProcess _uniqueResult = query.uniqueResult();
    return _uniqueResult;
  }
  
  @OneToMany(targetEntity = RCBasePurchasePayRequestImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "purchaseOrder")
  private Collection<RCBasePurchasePayRequest> payRequests = new java.util.ArrayList<com.shrct.businessprocess.purchase.RCBasePurchasePayRequest>();;
  
  public Collection<RCBasePurchasePayRequest> getPayRequests() {
    return this.payRequests;
  }
  
  public RCBasePurchasePayRequest createAndAddtoPayRequests() {
    getPayRequests().size();
    com.shrct.businessprocess.purchase.RCBasePurchasePayRequest rCBasePurchasePayRequest = new com.shrct.businessprocess.purchase.impl.RCBasePurchasePayRequestImpl(this);
    getObjectFactory().create(rCBasePurchasePayRequest);
    getPayRequests().add(rCBasePurchasePayRequest);
    return rCBasePurchasePayRequest;
    
  }
  
  public RCBasePurchasePayRequest createAndAddtoPayRequests(final Procedure1<RCBasePurchasePayRequest> updater) {
    getPayRequests().size();
    com.shrct.businessprocess.purchase.RCBasePurchasePayRequest rCBasePurchasePayRequest = new com.shrct.businessprocess.purchase.impl.RCBasePurchasePayRequestImpl(this);
    getObjectFactory().create(rCBasePurchasePayRequest);
    getPayRequests().add(rCBasePurchasePayRequest);
    return rCBasePurchasePayRequest;
    
  }
  
  public RCBasePurchaseOrder removeAllPayRequests() {
    for(RCBasePurchasePayRequest obj : this.payRequests){
    	getObjectFactory().delete(obj);
    }
    this.payRequests.clear();
    return this;	
    
  }
  
  public RCBasePurchaseOrder removeFromPayRequests(final RCBasePurchasePayRequest rCBasePurchasePayRequest) {
    this.payRequests.remove(rCBasePurchasePayRequest);
    getObjectFactory().delete(rCBasePurchasePayRequest);
    return this;
    
  }
  
  @OneToMany(targetEntity = RCPurchaseOrderInvoiceImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "purchaseOrder")
  private Collection<RCPurchaseOrderInvoice> invoices = new java.util.ArrayList<com.shrct.businessprocess.purchase.RCPurchaseOrderInvoice>();;
  
  public Collection<RCPurchaseOrderInvoice> getInvoices() {
    return this.invoices;
  }
  
  public RCPurchaseOrderInvoice createAndAddtoInvoices() {
    getInvoices().size();
    com.shrct.businessprocess.purchase.RCPurchaseOrderInvoice rCPurchaseOrderInvoice = new com.shrct.businessprocess.purchase.impl.RCPurchaseOrderInvoiceImpl(this);
    getObjectFactory().create(rCPurchaseOrderInvoice);
    getInvoices().add(rCPurchaseOrderInvoice);
    return rCPurchaseOrderInvoice;
    
  }
  
  public RCPurchaseOrderInvoice createAndAddtoInvoices(final Procedure1<RCPurchaseOrderInvoice> updater) {
    getInvoices().size();
    com.shrct.businessprocess.purchase.RCPurchaseOrderInvoice rCPurchaseOrderInvoice = new com.shrct.businessprocess.purchase.impl.RCPurchaseOrderInvoiceImpl(this);
    getObjectFactory().create(rCPurchaseOrderInvoice);
    getInvoices().add(rCPurchaseOrderInvoice);
    return rCPurchaseOrderInvoice;
    
  }
  
  public RCBasePurchaseOrder removeAllInvoices() {
    for(RCPurchaseOrderInvoice obj : this.invoices){
    	getObjectFactory().delete(obj);
    }
    this.invoices.clear();
    return this;	
    
  }
  
  public RCBasePurchaseOrder removeFromInvoices(final RCPurchaseOrderInvoice rCPurchaseOrderInvoice) {
    this.invoices.remove(rCPurchaseOrderInvoice);
    getObjectFactory().delete(rCPurchaseOrderInvoice);
    return this;
    
  }
  
  public RCExchangeRate getDefaultExchangeRate() {
    return this.exchangeRate;
  }
  
  public String getEntityName() {
    return this.orderNumber;
  }
  
  public RCProject getProject() {
    RCBudget _budget = this.getBudget();
    RCCostBreakDown _cbd = _budget.getCbd();
    RCProject _project = _cbd.getProject();
    return _project;
  }
  
  public RCPurchaseOrderInvoice createInvoice() {
    RCPurchaseOrderInvoice _createAndAddtoInvoices = this.createAndAddtoInvoices();
    final Procedure1<RCPurchaseOrderInvoice> _function = new Procedure1<RCPurchaseOrderInvoice>() {
        public void apply(final RCPurchaseOrderInvoice it) {
          RCBasePurchaseOrderImpl.this.invoiceRegister();
        }
      };
    RCPurchaseOrderInvoice _doubleArrow = ObjectExtensions.<RCPurchaseOrderInvoice>operator_doubleArrow(_createAndAddtoInvoices, _function);
    return _doubleArrow;
  }
  
  public RCPurchasePayRequest createPayRequest() {RCPurchasePayRequest rCPurchasePayRequest=RCPurchasePayRequestImpl.create(this,getObjectFactory());
    final Procedure1<RCPurchasePayRequest> _function = new Procedure1<RCPurchasePayRequest>() {
        public void apply(final RCPurchasePayRequest it) {
          RCBudget _budget = it.getBudget();
          it.setThebudget(_budget);
          Date _date = new Date();
          it.setTheStartDate(_date);
          RCBasePurchaseOrderImpl.this.payRequest();
        }
      };
    RCPurchasePayRequest _doubleArrow = ObjectExtensions.<RCPurchasePayRequest>operator_doubleArrow(rCPurchasePayRequest, _function);
    return _doubleArrow;
  }
  
  /**
   * 结存预付款
   */
  public RCProjectMoney getRemainPayAmount() {
    Iterable<RCPurchasePayRequest> _filter = Iterables.<RCPurchasePayRequest>filter(this.payRequests, RCPurchasePayRequest.class);
    final Function1<RCPurchasePayRequest,RCProjectMoney> _function = new Function1<RCPurchasePayRequest,RCProjectMoney>() {
        public RCProjectMoney apply(final RCPurchasePayRequest it) {
          RCProjectMoney _remainAmount = it.getRemainAmount();
          return _remainAmount;
        }
      };
    Iterable<RCProjectMoney> _map = IterableExtensions.<RCPurchasePayRequest, RCProjectMoney>map(_filter, _function);
    RCExchangeRate _defaultExchangeRate = this.getDefaultExchangeRate();
    RCProjectMoney _sum = Moneys.sum(_map, _defaultExchangeRate);
    return _sum;
  }
  
  public RCBasePurchaseOrderProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCBasePurchaseOrderProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCBasePurchaseOrderProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
